<template>
    <div class="addPopup">
        <div class="add-close"><a @click="close"></a></div>
        <ul class="add-list">
            <li>
                <p>姓名</p>
                <input type="text" v-model.trim="address.userName">
            </li>
            <li>
                <p>电话</p>
                <input type="text" v-model.trim="address.mobileNo">
            </li>
            <li>
                <p>地址</p>
                <input type="text" v-model.trim="address.address">
            </li>
        </ul>
        
        <div v-if="this.address.addressId" class="add-check">            
            <label>设为默认地址</label>
            <input type="checkbox" :true-value='1' :false-value='0' v-model="address.status" :class="{'on' : address.status==1}">
        </div>
        <button type="submit" @click="hiddenShare()">确 认</button>
    </div>
</template>

<script>
import { MessageBox } from 'mint-ui';
import {mapActions} from 'vuex'
export default {
    props:{
        address:{
            type:Object,
            required:true,
            default(){
                return {}
            }
        }
    },
    methods:{
        ...mapActions({
            addCommonAddress:'personModules/addCommonAddress',
            updateCommonAddress:'personModules/updateCommonAddress'
        }),
        hiddenShare(){
            if(!this.address.userName||!this.address.mobileNo||!this.address.address){
                MessageBox('提示', '选项不能为空');
                return;
            }
            if(this.address.userName && this.address.userName.length>8){
                MessageBox('提示', '用户名过长重新输入');
                this.address.userName = '';
                return;
            }
            if(this.address.mobileNo && (this.address.mobileNo.length != 11 || isNaN(this.address.mobileNo))){
                MessageBox('提示', '无效的手机号码');
                this.address.mobileNo = '';
                return;
            }
            if(this.address.address && this.address.address.length>100){
                MessageBox('提示', '地址过长重新输入');
                this.address.address = '';
                return;
            }
            this.$emit("close");        
            if(!this.address.addressId){
                this.addCommonAddress(this.address);
            }else{
                // console.log(this.address);  1
                // return;
                this.updateCommonAddress(this.address);
                // console.log(this.address); 0
                // return;
            }
            this.$emit('clearAddress');
            // this.addCommonAddress(this.address);
            // this.address = {};
        },
        close(){
            this.$emit("close");
            //执行获取数据
            this.$store.dispatch('personModules/getCommonAddress');
            // this.$router.go(0);
        }
    }
}
</script>

<style lang="scss" scoped>
@import "../assets/style/order.scss";
</style>


